package com.ws.util;

import com.ws.dao.BikeManager;

import java.util.InputMismatchException;
import java.util.Scanner;

public class Menu {
    private final Scanner scanner = new Scanner(System.in);
    private BikeManager bikeManager;

    // 构造函数，用于初始化BikeManager
    public Menu(BikeManager bikeManager) {
        this.bikeManager = bikeManager;
    }

    public void startMenu() {
        while (true) {
            System.out.println("——————————————————————————————");
            System.out.println("\uF0751:投放Bike\n" +
                    "\uF0752:查看Bike\n" +
                    "\uF0753:删除Bike\n" +
                    "\uF0754:借出Bike\n" +
                    "\uF0755:归还Bike\n" +
                    "\uF0756:Bike借出排行榜\n" +
                    "\uF0757:退出");
            System.out.println("——————————————————————————————");
            System.out.print("请选择:");
            try {
                int i = scanner.nextInt();
                switch (i) {
                    case 1:
                        bikeManager.dropBike();
                        break;
                    case 2:
                        bikeManager.viewBike();
                        break;
                    case 3:
                        bikeManager.deleteBike();
                        break;
                    case 4:
                        bikeManager.loanBike();
                        break;
                    case 5:
                        bikeManager.returnBike();
                        break;
                    case 6:
                        bikeManager.bikeranking();
                        break;
                    case 7:
                        System.out.println("————>欢迎下次使用<————");
                        System.exit(0);
                        break;
                    default:
                        System.out.println("你输入的数据错误");
                        break;
                }
            } catch (InputMismatchException e) {
                System.out.println("请输入数字");
                scanner.next();
            }
            returnMain(); // 调用非静态的returnMain方法
        }
    }

    // 非静态方法，用于返回主菜单
    public void returnMain() {
        boolean shouldReturn = false;
        while (!shouldReturn) {
            System.out.println("输入0返回上一级:");
            try {
                int i = scanner.nextInt();
                if (i == 0) {
                    shouldReturn = true;
                } else {
                    System.out.println("你的输入有误，请输入0返回上一级");
                }
            } catch (InputMismatchException e) {
                System.out.println("请输入数字");
                scanner.next();
            }
        }
    }
}